<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<body>
    <div class="card">
        <div class="card-header d-flex justify-content-between align-items-center">
            <h5 class="mb-0">专业管理</h5>
            <button class="btn btn-primary" onclick="openMajorModal()">
                <i class="fas fa-plus"></i> 添加专业
            </button>
        </div>
        <div class="card-body">
            <table class="table table-hover">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>专业名称</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    <tr th:each="major : ${majors}">
                        <td th:text="${major.id}"></td>
                        <td th:text="${major.majorName}"></td>
                        <td>
                            <button class="btn btn-sm btn-warning btn-action" th:onclick="'editMajor(' + ${major.id} + ')'">
                                <i class="fas fa-edit"></i>
                            </button>
                            <button class="btn btn-sm btn-danger btn-action" th:onclick="'deleteMajor(' + ${major.id} + ')'">
                                <i class="fas fa-trash"></i>
                            </button>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>

    <!-- 添加/编辑专业模态框 -->
    <div class="modal fade" id="majorModal" tabindex="-1">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">专业信息</h5>
                    <button type="button" class="close" data-dismiss="modal">
                        <span>&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <form id="majorForm">
                        <input type="hidden" id="majorId">
                        <div class="form-group">
                            <label>专业名称</label>
                            <input type="text" class="form-control" id="majorName" required>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary" onclick="saveMajor()">保存</button>
                </div>
            </div>
        </div>
    </div>

    <script>
        function openMajorModal(id) {
            if (id) {
                $.get('/major/' + id, function(data) {
                    $('#majorId').val(data.id);
                    $('#majorName').val(data.majorName);
                    $('#majorModal').modal('show');
                });
            } else {
                $('#majorForm')[0].reset();
                $('#majorId').val('');
                $('#majorModal').modal('show');
            }
        }

        function saveMajor() {
            var major = {
                id: $('#majorId').val(),
                majorName: $('#majorName').val()
            };

            var url = major.id ? '/major' : '/major';
            var method = major.id ? 'PUT' : 'POST';

            $.ajax({
                url: url,
                type: method,
                contentType: 'application/json',
                data: JSON.stringify(major),
                success: function() {
                    $('#majorModal').modal('hide');
                    loadContent('major');
                }
            });
        }

        function deleteMajor(id) {
            if(confirm('确定要删除这个专业吗？')) {
                $.ajax({
                    url: '/major/' + id,
                    type: 'DELETE',
                    success: function() {
                        loadContent('major');
                    }
                });
            }
        }
    </script>
</body>
</html> 